package main

import (
	"log"
	"os"
	"os/signal"
	"strconv"
	"syscall"
	"time"
)

func main() {
	log.Println("Starting EbbFlow WaterBear + Epoch Management Server")
	
	// 获取节点ID
	nodeID := 0
	if len(os.Args) > 2 && os.Args[1] == "-id" {
		if id, err := strconv.Atoi(os.Args[2]); err == nil {
			nodeID = id
		}
	}
	
	log.Printf("Node ID: %d", nodeID)
	log.Printf("Total Nodes: 4, Faulty Nodes: 1")
	
	// 模拟WaterBear初始化
	log.Println("Initializing WaterBear component...")
	time.Sleep(1 * time.Second)
	log.Println("WaterBear initialized successfully")
	
	// 模拟Epoch管理初始化
	log.Println("Initializing Epoch Management...")
	time.Sleep(500 * time.Millisecond)
	log.Println("Epoch Management initialized successfully")
	
	// 启动Epoch管理循环
	go func() {
		epochCount := 0
		for {
			epochCount++
			log.Printf("Starting Epoch %d", epochCount)
			
			// 模拟ACS执行
			log.Printf("Executing ACS in Epoch %d", epochCount)
			time.Sleep(2 * time.Second)
			log.Printf("ACS completed in Epoch %d", epochCount)
			
			// 模拟WaterBear共识
			log.Printf("Executing WaterBear consensus in Epoch %d", epochCount)
			time.Sleep(1 * time.Second)
			log.Printf("WaterBear consensus completed in Epoch %d", epochCount)
			
			// 每3个epoch执行一次ADKR（但不实际执行）
			if epochCount%3 == 0 {
				log.Printf("ADKR scheduled for Epoch %d (but not executed)", epochCount)
			}
			
			log.Printf("Epoch %d completed", epochCount)
			
			// 如果运行了10个epoch就停止
			if epochCount >= 10 {
				break
			}
			
			time.Sleep(1 * time.Second)
		}
		log.Println("Epoch management completed")
	}()
	
	log.Println("WaterBear + Epoch Management server started successfully")
	
	// 等待中断信号
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
	<-sigCh
	
	log.Println("Shutting down WaterBear + Epoch Management server...")
}